import 'package:flutter/material.dart';
import 'package:flutter_openim_sdk/flutter_openim_sdk.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:openim_common/openim_common.dart';

class ChatCardView extends StatelessWidget {
  const ChatCardView({
    Key? key,
    required this.message,
    this.isISend = false,
    this.onTapCard,
    this.isMultiSelectMode = false,
    this.onToggleSelection,
  }) : super(key: key);

  final Message message;
  final bool isISend;
  final Function(String userID, String nickname, String? faceURL)? onTapCard;
  final bool isMultiSelectMode;
  final Function(Message message)? onToggleSelection;

  /// 获取好友备注信息
  Future<String?> _getFriendRemark(String? userID) async {
    if (userID == null || userID.isEmpty) return null;
    
    try {
      final friendInfo = (await OpenIM.iMManager.friendshipManager.getFriendsInfo(
        userIDList: [userID],
      )).firstOrNull;
      
      return friendInfo?.remark;
    } catch (e) {
      print('获取好友备注失败: $e');
      return null;
    }
  }

  @override
  Widget build(BuildContext context) {
    final cardElem = message.cardElem;
    if (cardElem == null) {
      return Container(
        padding: EdgeInsets.all(12.w),
        child: Text(
          '[名片消息解析失败]',
          style: TextStyle(
            color: isISend ? Styles.c_FFFFFF : Styles.c_0C1C33,
            fontSize: 14.sp,
          ),
        ),
      );
    }

    return GestureDetector(
      onTap: () {
        if (isMultiSelectMode) {
          onToggleSelection?.call(message);
        } else {
          onTapCard?.call(
            cardElem.userID ?? '',
            cardElem.nickname ?? '',
            cardElem.faceURL,
          );
        }
      },
      child: Container(
        constraints: BoxConstraints(
          maxWidth: 240.w,
        ),
        padding: EdgeInsets.all(12.w),
        decoration: BoxDecoration(
          color: isISend ? Styles.c_0089FF.withOpacity(0.1) : Styles.c_F2F8FF,
          borderRadius: BorderRadius.circular(8.r),
          border: Border.all(
            color: isISend ? Styles.c_0089FF.withOpacity(0.2) : Styles.c_E8EAEF,
            width: 1.w,
          ),
        ),
        child: IntrinsicWidth(
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              // 头像
              AvatarView(
                width: 40.w,
                height: 40.w,
                url: cardElem.faceURL,
                text: cardElem.nickname,
                borderRadius: BorderRadius.circular(20.r),
                showDefaultAvatar: true,
              ),
              12.horizontalSpace,
              // 用户信息
              Flexible(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    // 昵称
                    Text(
                      cardElem.nickname ?? '未知用户',
                      style: TextStyle(
                        color: isISend ? Styles.c_0C1C33 : Styles.c_0C1C33,
                        fontSize: 16.sp,
                        fontWeight: FontWeight.w500,
                      ),
                      maxLines: 1,
                      overflow: TextOverflow.ellipsis,
                    ),
                    // 用户ID
                    if (cardElem.userID?.isNotEmpty == true) ...[
                      4.verticalSpace,
                      Text(
                        'ID: ${cardElem.userID}',
                        style: TextStyle(
                          color: Styles.c_8E9AB0,
                          fontSize: 12.sp,
                        ),
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                      ),
                    ],
                    // 好友备注
                    FutureBuilder<String?>(
                      future: _getFriendRemark(cardElem.userID),
                      builder: (context, snapshot) {
                        final remark = snapshot.data;
                        if (remark != null && remark.isNotEmpty) {
                          return Column(
                            crossAxisAlignment: CrossAxisAlignment.start,
                            children: [
                              4.verticalSpace,
                              Text(
                                '备注: $remark',
                                style: TextStyle(
                                  color: Styles.c_8E9AB0,
                                  fontSize: 12.sp,
                                ),
                                maxLines: 1,
                                overflow: TextOverflow.ellipsis,
                              ),
                            ],
                          );
                        }
                        return const SizedBox.shrink();
                      },
                    ),
                  ],
                ),
              ),
              8.horizontalSpace,
              // 名片标识
              Container(
                padding: EdgeInsets.symmetric(horizontal: 6.w, vertical: 2.h),
                decoration: BoxDecoration(
                  color: Styles.c_0089FF.withOpacity(0.1),
                  borderRadius: BorderRadius.circular(4.r),
                ),
                child: Text(
                  '名片',
                  style: TextStyle(
                    color: Styles.c_0089FF,
                    fontSize: 10.sp,
                    fontWeight: FontWeight.w500,
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}